package com.servlet.users;

import java.io.IOException;
import java.util.List;
import java.util.ListIterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.ArticleDAO;
import com.dao.UsersDAO;
import com.entity.Article;
import com.entity.Users;

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String userName = request.getParameter("user_name");
		String userPassword = request.getParameter("user_password");
		UsersDAO usersDAO = new UsersDAO();
		List<Users> users = usersDAO.findAll();
		for (Users u : users) {
			if (u.getUser_name().equals(userName)
					&& u.getUser_password().equals(userPassword)) {
				System.out.println("success");
				ArticleDAO articleDAO=new ArticleDAO();
                List<Article> article= articleDAO.findByUserId(u.getUser_id());
                System.out.println(u.getUser_id());
                request.setAttribute("article", article);
				request.getRequestDispatcher("/homepage.jsp").forward(request,
						response);
				
				return;
			}

			request.getRequestDispatcher("/error.jsp").forward(request,
					response);

		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String userName = request.getParameter("user_name");
		String userPassword = request.getParameter("user_password");
		UsersDAO usersDAO = new UsersDAO();
		List<Users> users = usersDAO.findAll();
		for (Users u : users) {
			if (u.getUser_name().equals(userName)
					&& u.getUser_password().equals(userPassword)) {
				System.out.println("success");
				ArticleDAO articleDAO=new ArticleDAO();
                List<Article> article= articleDAO.findByUserId(u.getUser_id());
                System.out.println(u.getUser_id());
                request.setAttribute("article", article);
				request.getRequestDispatcher("/homepage.jsp").forward(request,
						response);
				
				return;
			}

			request.getRequestDispatcher("/error.jsp").forward(request,
					response);

		}
	}
}
